#ifndef _MQTT_CLIENT_H_
#define _MQTT_CLIENT_H_

#include<string>

#include <mqtt/async_client.h>
#include <mqtt/create_options.h>
#include <mqtt/types.h>

class MqttBase {
 public:
  MqttBase(const std::string& broker_addr, const std::string& client_name, const std::string& topic, int qos = 1);
  virtual ~MqttBase();
  virtual void Run() = 0;

 protected:
  mqtt::async_client client_;       // MQTT异步客户端
  mqtt::connect_options con_opts_;  // 连接选项
  std::string topic_;               // 订阅的主题
  int qos_;                         // QoS等级
};

class PubCallback : public mqtt::callback {
public:
    // 处理发布确认的回调
    void delivery_complete(mqtt::delivery_token_ptr token) override;
};


class MqttPublisher : public MqttBase {
 public:
  MqttPublisher(const std::string& broker_addr, const std::string& client_name, const std::string& topic, int qos = 1);
  void Run() override;
 private:
  void Publish();
  std::string GenerateMessage();

  PubCallback call_back_;
};

class SubCallBack : public mqtt::callback {
 public:
  //  消息到来时的回调函数
  void message_arrived(mqtt::const_message_ptr msg) override;
  //  连接断开时的回调函数
  void connection_lost(const std::string& cause) override;
};

class MqttSubscriber : public MqttBase {
 public:
  MqttSubscriber(const std::string& broker_addr, const std::string& client_name, const std::string& topic, int qos = 1);
  void Run() override;

 private:
  void Subscribe();

  SubCallBack call_back_;
};
#endif